package authentication.module;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import javax.security.auth.login.LoginContext;
import javax.security.auth.login.LoginException;

import authentication.module.SubjectCallbackHandler;

public class LoginDriver {

	public static void main(String[] args) throws IOException {
		System.setProperty("java.security.auth.login.config", "psiexamauth.cfg");
		LoginContext subjectLoginContext = null;
		LoginContext userLoginContext = null;
		// the authentication module determines if a subject or an user
		// (psychologist) wants to authenticate - thus, processing the proper
		// login context
		System.out.println("Who are you: <user> or <subject>?");
		String access = new BufferedReader(new InputStreamReader(System.in)).readLine();
		if (access.equals("subject")){
			try {
				subjectLoginContext = new LoginContext(
						"PsiExamSubjectAuthentication",
						new SubjectCallbackHandler());
				/*
				 * userLoginContext = new LoginContext("PsiExamUserAuthentication",
				 * new UserCallbackHandler());
				 */
			} catch (LoginException e) {
				System.out.println(e.getMessage());
				System.exit(0);

			}
			while (true) {
				try {
					subjectLoginContext.login();
					/* userLoginContext.login(); */
					System.exit(0);
				} catch (LoginException e) {
					System.out.println(e.getMessage());
				}

			}
		}else{
			if (access.equals("user")){
				try {
					/*subjectLoginContext = new LoginContext(
							"PsiExamSubjectAuthentication",
							new SubjectCallbackHandler());*/
					  userLoginContext = new LoginContext("PsiExamUserAuthentication",
					  new UserCallbackHandler());
					 
				} catch (LoginException e) {
					System.out.println(e.getMessage());
					System.exit(0);

				}
				while (true) {
					try {
						/*subjectLoginContext.login();*/
						 userLoginContext.login(); 
						System.exit(0);
					} catch (LoginException e) {
						System.out.println(e.getMessage());
					}
				}
			}else{
				System.out.println("Unknown access");
			}
		}
	

	}

}
